//
//  UILabel+EV.m
//  EVClub
//
//  Created by sam on 14-9-24.
//  Copyright (c) 2014年 BitRice. All rights reserved.
//
#import "UILabel+EV.h"
#import <objc/runtime.h>

static inline void swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

@implementation UILabel (EV)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool {
            swizzleSelector(self, @selector(text), @selector(ev_text));
            swizzleSelector(self, @selector(setText:), @selector(ev_setText:));
            swizzleSelector(self, @selector(setAttributedText:), @selector(ev_setAttributedText:));
        }
    });
}

- (BOOL)ev_enableEmojiDecode {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)ev_setEnableEmojiDecode:(BOOL)enableEmojiDecode {
    objc_setAssociatedObject(self, @selector(ev_enableEmojiDecode), @(enableEmojiDecode), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setEv_enableEmojiDecode:(BOOL)enableEmojiDecode {
    [self ev_setEnableEmojiDecode:enableEmojiDecode];
}

- (NSString *)ev_text {
    return [self ev_text];
}

- (void)ev_setText:(NSString *)text {
    [self ev_setText:text];
}

- (void)ev_setAttributedText:(NSAttributedString *)attributedText {
    [self ev_setAttributedText:attributedText];
}

+ (CGSize)ev_sizeForText:(NSString *)text size:(CGSize)size attributes:(NSDictionary *)attributes {
    if (![text length]){
        return CGSizeZero;
    }
    return [self ev_sizeForAttributedText:[[NSAttributedString alloc]initWithString:text] size:size attributes:attributes];
}

+ (CGSize)ev_sizeForAttributedText:(NSAttributedString *)text size:(CGSize)size attributes:(NSDictionary *)attributes {
    return [self ev_sizeForAttributedText:text size:size attributes:attributes enableEmojiDecode:YES];
}

+ (CGSize)ev_sizeForText:(NSString *)text size:(CGSize)size attributes:(NSDictionary *)attributes enableEmojiDecode:(BOOL)enableEmojiDecode {
    return [self ev_sizeForAttributedText:[[NSAttributedString alloc]initWithString:text] size:size attributes:attributes enableEmojiDecode:enableEmojiDecode];
}

+ (CGSize)ev_sizeForAttributedText:(NSAttributedString *)text size:(CGSize)size attributes:(NSDictionary *)attributes enableEmojiDecode:(BOOL)enableEmojiDecode {
    return [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
}


-(void) ev_adjustsWidthToFitText {
    [self ev_adjustsWidthToFitText:MAXFLOAT];
}

-(void) ev_adjustsWidthToFitText:(float)maxWidth {
    CGSize size = [self.text boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:0 attributes:self.font == nil ? nil : @{NSFontAttributeName:self.font} context:nil].size;
    CGRect frame = self.frame;
    frame.size.width = (int)(size.width) + 1;//会四舍五入，不加1，舍去小数点，显示可能就少已行了
    self.frame = frame;
}

-(void) ev_adjustsSizeToFitText:(float)maxWidth {
    CGSize size = [self.text boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:0 attributes:self.font == nil ? nil : @{NSFontAttributeName:self.font} context:nil].size;
    CGRect frame = self.frame;
    frame.size = CGSizeMake(size.width+1, size.height+1);//会四舍五入，不加1，舍去小数点，显示可能就少一行了
    self.frame = frame;
}

- (void)ev_adjustsHeightToFitText{
    CGSize size = [self.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:0 attributes:self.font == nil ? nil : @{NSFontAttributeName:self.font} context:nil].size;
    CGRect frame = self.frame;
    frame.size.height = size.height+1;//会四舍五入，不加1，舍去小数点，显示可能就少已行了
    self.frame = frame;
}
@end
